param ( $PowerShellSE = 'C:\Program Files\PowerShell SE\PowerShell_SE.exe' )

# Find PowerShell SE on a drive if default/ provided path is not correct
if (!($PowerShellSE -and (Test-Path $PowerShellSE))) {
    $PowerShellSE = Get-ChildItem c:\ -Recurse -Force -Filter 'PowerShell*.exe' -ErrorAction 0 | 
        where { $_.VersionInfo.ProductName -eq 'PowerShell SE' } | select -First 1 -ExpandProperty FullName
}
if ($PowerShellSE) { 
    New-Alias -Name SE -Value $PowerShellSE -Force
} else {
    Write-Warning 'PowerShell SE not found!'
}

Add-Type -Path .\ISEFun.dll

$SEFileList = New-object ISEFun.SEFileList


function Export-SEFile {
    if (!($file = $psISE.CurrentFile)) {
        Write-Error 'No file found'
        return
    }
    if ($file.IsSaved -and (Test-Path $file.FullPath)) {
        $Script:SEFileList.Add($file.FullPath,'Saved')
        $psISE.CurrentPowerShellTab.Files.Remove($file,$true)
        SE $file.FullPath
    } elseif (Test-Path $file.FullPath) {
        $value = "# Original path: $($file.FullPath)`n" + $file.Editor.Text
        $tempFile = New-Item -ItemType File -Path (Join-Path ([io.path]::GetTempPath()) (Split-Path -Leaf $file.FullPath)) -Value $Value
        $Script:SEFileList.Add($file.FullPath,$tempFile.FullName,'NotSaved')
        $psISE.CurrentPowerShellTab.Files.Remove($file,$true)
        SE $tempFile
        
    } else {
        $tempfile = New-Item -ItemType File -Path ( [io.path]::GetTempFileName() + '.ps1' ) -Value $($file.Editor.Text)
        $Script:SEFileList.Add($tempFile.FullName,'New')
        $psISE.CurrentPowerShellTab.Files.Remove($file,$true)
        SE $tempFile
    }
}   

function Get-SEFile {
    $Script:SEFileList.ToArray()
}

function Import-SEFile {
<#
    Probably need some help here...
#>
param (
    [Parameter(ValueFromPipelineByPropertyName = $true)]
    [Alias('Source','SP')]
    [string]$SourcePath,
    [Parameter(ValueFromPipelineByPropertyName = $true)]
    [Alias('Destination','DP')]
    [string]$DestinationPath
    )
process {
    foreach ($item in $Script:SEFileList) {
        if ($item.SourcePath -eq $SourcePath -or $item.DestinationPath -eq $DestinationPath) {
            if ($item.Type -eq 'New') {
                $file = $psISE.CurrentPowerShellTab.Files.Add()
                $file.Editor.InsertText($(Get-Content $DestinationPath | Out-String))
            } else {
                $psISE.CurrentPowerShellTab.Files.Add($item.DestinationPath) | Out-Null
            }
        }
    }
}
}
            
function Remove-SEFile {
param (
    [Parameter(ValueFromPipelineByPropertyName = $true)]
    [Alias('Source','SP')]
    [string]$SourcePath,
    [Parameter(ValueFromPipelineByPropertyName = $true)]
    [Alias('Destination','DP')]
    [string]$DestinationPath
    )
begin {
    $ItemsToRemove = New-Object ISEFun.SEFileList
}
process {
    foreach ($item in $Script:SEFileList) {
        if ($item.SourcePath -eq $SourcePath -or $item.DestinationPath -eq $DestinationPath) {
            switch ($item.Type) {
                'New' {
                    Remove-Item -Force $item.DestinationPath
                    $ItemsToRemove.Add($item)
                }
                'Saved' {
                    $ItemsToRemove.Add($item)
                }
                'NotSaved' {
                    switch ((Get-Item $item.SourcePath).LastWriteTime){
                        { $_ -gt (Get-Item $item.DestinationPath).LastWriteTime } { 
                            $reply = [windows.forms.MessageBox]::Show("Original file was modified after temporary copy, overwrite it?",
                                "Confirmation",
                                "YesNo",
                                "Question") 
                            break
                            }
                        { $_ -gt (Get-Item $item.Modified) } {
                            $reply = [windows.forms.MessageBox]::Show("Original file was modified after you created temporary copy, overwrite it?",
                                "Confirmation",
                                "YesNo",
                                "Question") 
                            break
                            }
                        default {
                            $reply = 'Yes'
                            }
                    }
                    if ($reply -eq 'Yes') {
                        Copy-Item $item.DestinationPath $item.SourcePath -Force
                    }           
                    Remove-Item -Force $item.DestinationPath
                    $ItemsToRemove.Add($item)
                }
            }
        }
    }
    foreach ($item in $ItemsToRemove) {
        $Script:SEFileList.Remove($item) | Out-Null
    }
}
}
